package com.easy.service.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ConcurrentModificationException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.easy.bean.Repair;
import com.easy.dao.RepairDao;
import com.easy.service.Service;
import com.easy.util.JDBC;

public class RepairService implements Service{
private RepairDao repairD=new RepairDao();
	
	public List<Repair> queryList(Map params){
		String checktext=(String)params.get("checktext");
		String selecttext=(String)params.get("selecttext");
		String sql="select * from h_repair where 1=1 ";
		try {
			if((!selecttext.equals(null)) && (!checktext.equals(null))) {
			sql+="and " + selecttext + " like " + "'%"+checktext+"%' ";
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		if(params.get("limit")!=null) {
			int limit=Integer.parseInt((String)params.get("limit"));
			int page=Integer.parseInt((String)params.get("page"));
			int start=(page-1)*limit;
			sql+="limit "+start+","+limit;
		}
		return repairD.querList(sql);
	}
	
	public Repair query(String repair_id) {
		String sql = "select repair_id from h_repair where repair_id='" + repair_id+"'";
		return repairD.query(sql);
	}

	@Override
	public Object query(Map params) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int queryCount(Map params) {
		String checktext=(String) params.get("checktext");
		String sql = "select count(*) from h_repair where 1=1";
		if(checktext != null) {
		sql+=" and repair_id like '%"+checktext+"%' ";
		sql+=" and r_id like '%"+checktext+"%' ";
		sql+=" or resp_id like '%"+checktext+"%' ";
		sql+=" or req_id like '%"+checktext+"%' ";
		}
		return repairD.queryCount(sql);
	}

	@Override
	public int update(Map params) {
		StringBuilder strB = new StringBuilder();
		strB.append("update h_repair set ");
		strB.append("repair_id='");
		strB.append(params.get("repair_id"));
		strB.append("',r_id='");
		strB.append(params.get("r_id"));
		strB.append("',resp_id='");
		strB.append(params.get("resp_id"));
		strB.append("',req_id='");
		strB.append(params.get("req_id"));
		strB.append("',question='");
		strB.append(params.get("question"));
		strB.append("',feedback='");
		strB.append(params.get("feedback"));
		strB.append("',rp_state='");
		strB.append(params.get("rp_state"));
		strB.append("',req_date='");
		strB.append(params.get("req_date"));
		strB.append("',resp_date='");
		strB.append(params.get("resp_date"));
		strB.append("' where ");
		strB.append("repair_id='");
		strB.append(params.get("repair_id"));
		strB.append("'");
		int count = repairD.update(strB.toString());
		return count;
	}

	@Override
	public int delete(Map params) {
		StringBuilder strB = new StringBuilder();
		strB.append("delete from h_repair where repair_id='");
		strB.append(params.get("repair_id"));
		strB.append("'");
		int count = repairD.delete(strB.toString());
		return count;
	}

	@Override
	public int insert(Map params) {
		//使用事务确保数据的完整性
		Connection con = null;
		try {
			con = JDBC.getConnection();
			con.setAutoCommit(false);
			StringBuilder strB = new StringBuilder();
			strB.append("insert into h_repair(repair_id,r_id,resp_id,req_id,question,feedback,rp_state,req_date,resp_date) values(");
			strB.append("'");
			strB.append(params.get("repair_id"));
			strB.append("','");
			strB.append(params.get("r_id"));
			strB.append("','");
			strB.append(params.get("resp_id"));
			strB.append("','");
			strB.append(params.get("req_id"));
			strB.append("','");
			strB.append(params.get("question"));
			strB.append("','");
			strB.append(params.get("feedback"));
			strB.append("','");
			strB.append(params.get("rp_state"));
			strB.append("','");
			strB.append(params.get("req_date"));
			strB.append("','");
			strB.append(params.get("resp_date"));
			strB.append("'");
			strB.append(")");
			repairD.insert(strB.toString());
			//保存报修单明细
			Object rinf_id=params.get("rinf_id");
			System.out.println(params.get("rinf_id"));
			Object rinf_goods=params.get("rinf_goods");
			System.out.println(rinf_goods.toString());
			Object rinf_question=params.get("rinf_question");
			System.out.println(rinf_question.toString());
			Object rinf_rid=params.get("rinf_rid");
			System.out.println(rinf_rid.toString());
			Object rinf_state=params.get("rinf_state");
			System.out.println(rinf_state.toString());
			String info_sql="";
			if(rinf_id.getClass().isArray()) {
				Integer[] arr_id = (Integer[])rinf_id;
				String[] arr_goods=(String[])rinf_goods;
				String[] arr_question=(String[])rinf_question;
				Integer[] arr_rid = (Integer[])rinf_rid;
				String[] arr_state = (String[])rinf_state;
				for(int i=0;i<arr_id.length;i++) {
					info_sql="insert into h_repairinfo(rinf_id,r_id,rinf_goods,rinf_question,rinf_rid,rinf_state) values("+arr_id[i]+",'"+params.get("repair_id")+",'"+arr_goods[i]+"','"+arr_question[i]+"','"+arr_rid[i]+"','"+arr_state[i]+"')";
					repairD.insertlist(con,info_sql);		
				}
			}else {
				info_sql="insert into h_repairinfo(rinf_id,r_id,rinf_goods,rinf_question,rinf_rid,rinf_state) values("+rinf_id+",'"+params.get("repair_id")+",'"+rinf_goods+"','"+rinf_question+"','"+rinf_rid+"','"+rinf_state+"')";
				System.out.println(info_sql);
				repairD.insertlist(con,info_sql);
			}
			
			con.commit();
			return 1;
			
		} catch (Exception  e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			JDBC.release(con, null, null);
			return 0;
		}
	}
}
